module.exports = (option, app) => {
    return async (ctx, next) => {
        // const { token } = ctx.header
        let token=ctx.header.token||ctx.query.token;
        console.log(token, "token")
        if (!token) {
            ctx.throw(400, "你没有权限访问该接口")
        }
        let user = {};
        try {
            user = ctx.checktoken(token)

        } catch (error) {

            console.log(error, "error")
            let fail = error.name === "TokenExpiredError"
                ? "token 已过期! 请重新获取令牌"
                : "Token 令牌不合法!";
            ctx.throw(400, fail)
        }
        let t = await ctx.service.cache.get("user_" + user.id)
        if (!t || t !== token) {
            ctx.throw(400, "Token 令牌不合法!")
        }
        user = await app.model.User.findByPk(user.id);
        if (!user || user.status == 0) {
            ctx.throw(400,"用户不存在或已被禁用")
        }
        ctx.authUser= user;
        console.log(ctx.authUser,"authuser")
        await next()
    }

}